var S1MPopover = (function($) {

	 // taken from mustache.js ----

	 var entityMap = {
			    "&": "&amp;",
			    "<": "&lt;",
			    ">": "&gt;",
			    '"': '&quot;',
			    "'": '&#39;',
			    "/": '&#x2F;'
			  };

	  function escapeHtml(string) {
	    return String(string).replace(/[&<>"'\/]/g, function (s) {
	      return entityMap[s];
	    });
	  }

	  // ---------------------------	
	
	function _generatePopover(divId, titleText, contentText, moreLink, titleResourceName, contentResourceName, moreLinkResourceName) //on init and on refresh upon Save
	  {   
		var htmlString = $('#popoverDivTemplate').html();
	    $('#' + divId).html(htmlString);
	    
	    var titleValue = '<table border=0 cellpadding=0 cellspacing=0 style="border-collapse: separate !important; display: block;"><tr style="display: table-row"><td style="padding-left: 0 !important; display: table-cell; background-color: #f5f5f5">' + ((titleText!=null) ? $.trim(escapeHtml(titleText)): '') + '</td><td valign=top style="padding-left: 0 !important;display: table-cell; background-color: #f5f5f5" width=2%><button type="button" class="close popover-close-btn" data-popover-class="popover-view-title_popover"> &times; </button></td></tr></table>';
	    var contentValue = (contentText!=null) ? $.trim(escapeHtml(contentText)) : '';
	    
	    moreLink = $.trim(moreLink);
	    if (moreLink != "")
	    {
	      if (moreLink.substring(0, 7) != "http://" && moreLink.substring(0, 8) != "https://")
	      {
	    	moreLink = "http://" + moreLink;
	      }
		
		  contentValue += '&nbsp;<a target="_blank" href="' 
	      + moreLink
	      + '" hidefocus="true"><div>'
	      + popover_learnMoreText
	      + '&nbsp;<i class="fa fa-external-link"></i></div></a>';
	    }
	    
	    $('#' + divId).find('i').attr("data-original-title", titleValue);
	    $('#' + divId).find('i').attr("data-content", contentValue);
	    
	    if (user.editPermission) //popover edit: populate Title and Content fields within Edit Content, set needed fields for submit
	    {
	      $('#editContentTemplate').find('input[name="POPOVER_TITLE_RESOURCE_NAME"]').attr("value", titleResourceName); //do not use val(), it doesn't change html
	      $('#editContentTemplate').find('input[name="POPOVER_CONTENT_RESOURCE_NAME"]').attr("value", contentResourceName); //do not use val(), it doesn't change html
	      $('#editContentTemplate').find('input[name="POPOVER_MORE_LINK_RESOURCE_NAME"]').attr("value", moreLinkResourceName); //do not use val(), it doesn't change html
	    
	      //title, content, more_link text inputs: setting their values
	      $('#editContentTemplate').find('input[name="POPOVER_TITLE"]').attr("value", titleText); //'Title' field - do not use val(), it doesn't change html
	      $('#editContentTemplate').find('textarea[name="POPOVER_CONTENT"]').text($.trim(contentText)); //'Content' field
	      $('#editContentTemplate').find('input[name="POPOVER_MORE_LINK"]').attr("value", moreLink); //'More' link
	    
	      var editHeaderText = popover_editHeaderText;
	      var editHeaderValue = editHeaderText + '<button type="button" id="editPopoverClose" class="close popover-close-btn" data-popover-class="popover-edit-title_popover"> &times; </button>';
	      var editContentValue = $('#editContentTemplate').html();
	    
	      $('#' + divId).find('a').attr("data-original-title", editHeaderValue); //edit popup header (this is not 'Title' field)
	      $('#' + divId).find('a').attr("data-content", editContentValue); //'Content' field
	    }
	 
	    if ($.trim(contentText) != "" || $.trim(moreLink) != ""  || user.editPermission) //according to SF TI-010714 TN-1000719
	    {
	      $('#' + divId).find('i').popover(); //View popover ('i' icon)
	    }
		else
		{
		  $('#' + divId).find('i').hide();
		}
	    $('#' + divId).find('a').popover(); //Edit popover
	    
	    return false;
	  };  	
	  return {
		    
		  generatePopover : _generatePopover,
		  escapeHtml: escapeHtml //SF-28718
	  };
})(jQuery);


function savePopover(el)
{
  popoverDivId = el.parentElement.parentElement.parentElement.id;
  popoverDivName = el.parentElement.parentElement.parentElement.getAttribute('data-name');
  var popoverTitle = el.parentElement.getElementsByTagName('input')[IParameterName.POPOVER_TITLE].value;
  var popoverContent = el.parentElement.getElementsByTagName('textarea')[IParameterName.POPOVER_CONTENT].value;
  var popoverMoreLink = el.parentElement.getElementsByTagName('input')[IParameterName.POPOVER_MORE_LINK].value;
  
  var titleResourceName = el.parentElement.getElementsByTagName('input')[IParameterName.POPOVER_TITLE_RESOURCE_NAME].value;
  var contentResourceName = el.parentElement.getElementsByTagName('input')[IParameterName.POPOVER_CONTENT_RESOURCE_NAME].value;
  var moreLinkResourceName = el.parentElement.getElementsByTagName('input')[IParameterName.POPOVER_MORE_LINK_RESOURCE_NAME].value;
  
  //save fields with AJAX
  var data = {};
  data[IParameterName.TAG_ACTION] = IActionName.AJAX_SAVE_POPOVER;
  data[IParameterName.POPOVER_TITLE] = popoverTitle;
  data[IParameterName.POPOVER_TITLE_RESOURCE_NAME] = titleResourceName;
  data[IParameterName.POPOVER_CONTENT] = popoverContent;
  data[IParameterName.POPOVER_CONTENT_RESOURCE_NAME] = contentResourceName;
  data[IParameterName.POPOVER_MORE_LINK] = popoverMoreLink;
  data[IParameterName.POPOVER_MORE_LINK_RESOURCE_NAME] = moreLinkResourceName;
  
  S1MUtils.call(data, savePopoverCallback, null);  //in callback change popover values
}


function savePopoverCallback(jsonObj)
{
  if (jsonObj && jsonObj[IParameterName.VIEW_OBJECT])
  {
    //refresh popover
    var popoverVO = jsonObj[IParameterName.VIEW_OBJECT];
    
    if (popoverDivName && popoverDivName != popoverDivId) //there are multiple instances of the same popover
    {
      //find all elements with id in format : popoverDivName_x where x is number, and refresh (re-generate) each popover
      var elements = $('div[id^=' + popoverDivName + ']');
      for (var i = 0; i < elements.length; i++)
      {
    	var divId = elements[i].id;
    	S1MUtils.generatePopover(divId, popoverVO.title, popoverVO.content, popoverVO.moreLink, popoverVO.titleResourceName, popoverVO.contentResourceName, popoverVO.moreLinkResourceName);
      }
    }
    else
    {
      S1MUtils.generatePopover(popoverDivId, popoverVO.title, popoverVO.content, popoverVO.moreLink, popoverVO.titleResourceName, popoverVO.contentResourceName, popoverVO.moreLinkResourceName);
    }
  }
  //otherwise do not refresh, keep popup opened with errors displayed - this is in case of error
}
